<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>添加模块</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" />
    <link rel="stylesheet" href="/static/ui/css/ui.css" />

</head>

<body>
    <form class="layui-form" action="" lay-filter="editform">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">模块名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" lay-verify="required" autocomplete="off"
                                placeholder="请输入模块名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">模块标志</label>
                        <div class="layui-input-block">
                            <input type="text" name="key" lay-verify="required" autocomplete="off" placeholder="请输入模块标志"
                                class="layui-input">
                            <div class="layui-form-mid layui-word-aux">不能和系统中的其他模块标志相同</div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">后台路径</label>
                        <div class="layui-input-block">
                            <select name="server_path">
                                <option value="group">group</option>
                                <option value="home">home</option>
                            </select>
                        </div>
                    </div>
                    <!--div class="layui-form-item">
                        <label class="layui-form-label">系统菜单</label>
                        <div class="layui-input-block">
                            <input type="radio" name="is_sys" value="0" title="系统">
                            <input type="radio" name="is_sys" checked value="1" title="租户">
                            <input type="radio" name="is_sys" checked value="2" title="会员">
                        </div>
                    </div-->
                    <input type="hidden" name="is_sys" value="1" />
                    <div id="rule-area">
                        <div class="layui-form-item">
                            <label class="layui-form-label">上级权限</label>
                            <div class="layui-input-block">
                                <button class="layui-btn" id="chooseRule">
                                    <span id="ruleText">选择权限</span>
                                    <i class="layui-icon layui-icon-down layui-font-12"></i>
                                </button>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">图标</label>
                            <div class="layui-input-block">
                                <input type="text" name="icon" autocomplete="off" class="layui-input icon-selected"
                                    value="layui-icon layui-icon-username" />

                            </div>
                        </div>

                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">主表</label>
                        <div class="layui-input-block">
                            <select name="tables_main" lay-search id="key-area">
                                <option value="">请选择表</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类表</label>

                        <div class="layui-input-inline" id="tables" style="display:none"></div>
                        <div class="layui-input-inline">
                            <input type="checkbox" name="type" lay-skin="switch" lay-filter="type" lay-text="分类|不分类">
                        </div>
                        <div class="layui-form-mid layui-word-aux">分类表应包含id/pid/title</div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">全局常量</label>
                        <div class="layui-input-block" id="params"></div>
                    </div>


                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="请输入内容" class="layui-textarea"></textarea>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <input type="hidden" name="pid" value="-1" id="pid">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
            </div>
        </div>
    </form>
    <script src="../config.js"></script>
    <script src="/static/layui/layui.js"></script>
    <script src="/static/ui/ui.js"></script>
    <script>
        layui.use([
            'form', 'layer', 'dropdown', 'iconSelectedPlus', 'selectM'
        ], function () {
            let form = layui.form,
                layer = layui.layer,
                $ = layui.$,
                dropdown = layui.dropdown,
                selectM = layui.selectM,
                iconSelectedPlus = layui.iconSelectedPlus,
                type = 2;

            iconSelectedPlus.render(".icon-selected", {
                //position : 'right',
                icons: iconSelectedPlus.icons.layui,
                simple: true
            });
            

            _get(layui, 'mod/addBefore', res => {
                console.log(res)
                //渲染表列表
                res.tableList
                    .forEach(d => {
                        //console.log(d)
                        $("#key-area").append(new Option(d.name, d.id));
                    });
                form.render("select");
                //多选标签-所有配置
                selectM({
                    //元素容器【必填】
                    elem: '#tables'
                    //候选数据【必填】
                    , data: res.tableList
                    //最多选中个数，默认5
                    , max: 1
                    //input的name 不设置与选择器相同(去#.)
                    , name: 'tables_more'
                    //值的分隔符
                    , delimiter: ','
                    //候选项数据的键名
                    , field: { idName: 'id', titleName: 'name' }
                });
                form.on('switch(type)', function (data) {
                    type = this.checked ? '1' : '2';
                    console.log(status)
                    if (type == 1) {
                        $('#tables').show();
                    } else {
                        $('#tables').hide();
                    }
                });
                selectM({
                    //元素容器【必填】
                    elem: '#params'
                    //候选数据【必填】
                    , data: res.params
                    //最多选中个数，默认5
                    , max: 20
                    //input的name 不设置与选择器相同(去#.)
                    , name: 'params'
                    //值的分隔符
                    , delimiter: ','
                    //候选项数据的键名
                    , field: { idName: 'id', titleName: 'name' }
                });
                //console.log(moreBtn)

                //渲染权限
                dropdown.render({
                    elem: '#chooseRule',
                    data: res.authTree,
                    trigger: 'hover',
                    click: (d, el) => {
                        //console.log(d)
                        $('#ruleText').text(d.title);
                        $('#pid').val(d.id);
                    }
                })
                //监听提交
                form.on('submit(user-save)', function (data) {
                    data = data.field;
                    if (data.tables_more && data.tables_main && data.tables_more.split(',').indexOf(data.tables_main) !== -1) {
                        layer.msg('附表中不能包含主表！');
                        return false;
                    }
                    //curd
                    // if(data.type == 5 && !data.tables_main) {
                    //     layer.msg('curd必须选择主表！');
                    //     return false;
                    // }
                    data.type = type;
                    if (type == 1 && !data.tables_more) {
                        layer.msg('请选择一个分类表！');
                        return false;
                    }
                    if (type == 2) {
                        data.table_more = '';
                    }
                    // if(data.iscate && data.iscate == 'on'){
                    //     //console.log(data.tables_more)
                    //     if(!data.tables_more) {
                    //         layer.msg('请选择一个附加表！');
                    //         return false;
                    //     }
                    //     if(data.tables_more.indexOf(',') !== -1) {
                    //         layer.msg('附加表为分类时只能选择一个！');
                    //         return false;
                    //     }
                    // }
                    // console.log(data)
                    // return false;
                    _post(layui, 'mod/add', data, rt => {
                        //console.log(res)
                        parent
                            .layui
                            .table
                            .reload("mod-table");
                        parent
                            .layer
                            .close(parent.layer.getFrameIndex(window.name)); //关闭当前页

                    })

                    return false;
                });
            });



        });
    </script>

</body>

</html>